﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ession;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace SalaryQuery
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string year;
        private AutoPost ap = new AutoPost();
        List<SalaryTotal> souce = new List<SalaryTotal>();
        List<SalaryMonth> MonthSalary = new List<SalaryMonth>();
        List<PriceMonth> OtherIncomes = new List<PriceMonth>();

        private string pCode;
        string postData;
        string result;
        private void btnbutton1_Click(object sender, EventArgs e)
        {

            year = cmbcomboBox1.Text;
            pCode = txttextBox1.Text;
            ThreadPool.QueueUserWorkItem(new WaitCallback(GetData));


        }

        private string getDataUrl = "http://192.168.85.30/ashx/NewSalaryQuery/GetData.ashx";
        private string otherPostData =
            "action=GetData&Title=%E4%BC%9A%E8%AE%A1%E5%B9%B4%E5%BA%A6&Columnchart=0&Sql_func=f_get_sal_other&Type_judge=1&Current_Year=2012&query_code_global=";
       
        private void GetData(object o)
        {
            // 获取数据,并保存.
            MonthSalary.Clear();
            OtherIncomes.Clear();
            souce.Clear();
            postData =
                //"[{\"Key\":\"_functionName\",\"Value\":\"GetUnChanged\"},{\"Key\":\"_prole\",\"Value\":\"N\"},{\"Key\":\"_pcode\",\"Value\":\"" +
                //pCode + "\"}]";
                "action=GetData&Title=%E5%B7%A5%E8%B5%84%E6%83%85%E5%86%B5&Columnchart=0&Sql_func=f_get_sal_gz&Type_judge=1&Current_Year=" + year + "&query_code_global=" +
                pCode;
            ;

            //action=GetData&Title=%E5%B7%A5%E8%B5%84%E6%83%85%E5%86%B5&Columnchart=0&Sql_func=f_get_sal_gz&Type_judge=1&Current_Year=2012&query_code_global=
            result = ap.PostData(getDataUrl, postData, Encoding.UTF8);


            if (!string.IsNullOrEmpty(result))
            {
                this.Invoke((Action)(() =>
                                      {
                                          rtbrichTextBox1.Text += "获取统计数据OK:" + pCode + ":" + year + "\r\n";
                                      }));

                var root = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result);
                var t = root["root"];
                foreach (var v in t)
                {
                    var s = v.ToObject<SalaryMonth>();
                    MonthSalary.Add(s);
                }

                this.Invoke((Action)(() =>
                                     {
                                         dataGridView2.DataSource = null;
                                         dataGridView2.DataSource = MonthSalary;
                                     }));
                //   var s = Newtonsoft.Json.JsonConvert.DeserializeObject<StaticsSet[]>(t);

            }
            else
            {
                MessageBox.Show("无数据");

            }

            //查询每年的工资统计
           Thread.Sleep(2000);
            ap = new AutoPost();
            postData =
                "action=GetData&Title=%E4%BC%9A%E8%AE%A1%E5%B9%B4%E5%BA%A6%E6%94%B6%E5%85%A5&Columnchart=0&Type_judge=1&Current_Year=2011&Sql_func=f_get_sal_year_fin&query_code_global=" +
                pCode;
            result = ap.PostData(getDataUrl, postData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(result))
            {
                this.Invoke((Action)(() =>
                                         {
                                             rtbrichTextBox1.Text += "获取年度数据OK:" + pCode + ":" + year + "\r\n";
                                         }));


                    var ss = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result);
                    var t = ss["root"];
                    foreach (var v in t)
                    {
                        var s = v.ToObject<SalaryTotal>();
                        souce.Add(s);
                    }

                    this.Invoke((Action)(() =>
                                         {
                                             dataGridView1.DataSource = null;
                                             dataGridView1.DataSource = souce;
                                         }));

                //    var jixiaos = Newtonsoft.Json.JsonConvert.DeserializeObject<OtherIncomeSet[]>(jixiao);


            }



            Thread.Sleep(1000);
            ap = new AutoPost();
            postData ="action=GetData&Title=%E4%BC%9A%E8%AE%A1%E5%B9%B4%E5%BA%A6&Columnchart=0&Sql_func=f_get_sal_other&Type_judge=1&Current_Year="+year+"&query_code_global="+
                    
                pCode;
            result = ap.PostData(getDataUrl, postData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(result))
            {
                this.Invoke((Action)(() =>
                                         {
                                             rtbrichTextBox1.Text += "获取其他收入数据OK:" + pCode + ":" + year + "\r\n";
                                         }));
                

                var ss = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result);
                var t = ss["root"];
                foreach (var v in t)
                {
                    var s = v.ToObject<PriceMonth>();
                    OtherIncomes.Add(s);
                }

                this.Invoke((Action)(() =>
                                     {
                                         dataGridView3.DataSource = null;
                                         dataGridView3.DataSource = OtherIncomes;
                                     }));

                //    var jixiaos = Newtonsoft.Json.JsonConvert.DeserializeObject<OtherIncomeSet[]>(jixiao);


            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                cmbcomboBox1.SelectedIndex = 3;

                Init();
            }
            catch (Exception)
            {


            }

        }
        EssionEntities1 en = new EssionEntities1();
        private DepartmentsNew selDep;
        List<DepartmentsNew> depList;
        private List<Employee> emps;
        private void Init()
        {
            //加载部门列表
            //var q = from department in en.DepartmentsNew
            //        select department;


            depList = en.DepartmentsNew.ToList();
            cmbcomboBox2.DataSource = depList;
            cmbcomboBox2.DisplayMember = "ORGAN_NAME";



        }

        private void cmbcomboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //  comboBox1.Items.Clear();
            comboBox1.SelectedIndex = -1;
            selDep = cmbcomboBox2.SelectedItem as DepartmentsNew;
            if (selDep != null)
            {
                var q = (from dep in en.DepartmentsNew

                         where dep.ORGAN_NAME == selDep.ORGAN_NAME
                         select dep).Select(qq => qq.ORGAN_ID).ToList();

                var query = from employee in en.Employees

                            where q.Contains(employee.C_ORGAN_ID_)


                            select employee;
                emps = query.ToList();

                //   MessageBox.Show((query as ObjectQuery<Employee>).ToTraceString());  
                comboBox1.DataSource = emps;
                comboBox1.DisplayMember = "C_NAME_";
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                var name = comboBox1.SelectedValue as Employee;

                if (name != null)
                {
                    var q = en.Employees.Where(es => es.C_NAME_ == name.C_NAME_).FirstOrDefault();
                    txttextBox1.Text = q.C_P_CODE_;
                }

            }


        }

        private List<DepartmentsNew> tmpDeps;
        private void cmbcomboBox2_TextUpdate(object sender, EventArgs e)
        {

        }

        private void txttextBox2_TextChanged(object sender, EventArgs e)
        {
            string s = txttextBox2.Text;
            if (s == "")
            {
                cmbcomboBox2.DataSource = depList;
            }
            else
            {
                tmpDeps = depList.Where(d => d.FULL_PATH.Contains(s) || d.NAME_JP.Contains(s)).ToList();
                cmbcomboBox2.DataSource = tmpDeps;
            }

        }

        private List<Employee> tmpEMPs;
        private void txttextBox3_TextChanged(object sender, EventArgs e)
        {
            string s = txttextBox3.Text;
            if (s == "")
            {
                comboBox1.DataSource = emps;
            }
            else
            {
                tmpEMPs = en.Employees.Where(d => d.C_NAME_JP_.Contains(s) || d.C_NAME_.Contains(s) || d.C_NAME_QP_.Contains(s)).ToList();
                comboBox1.DataSource = tmpEMPs;
            }
        }

        List<string> pcodes = new List<string>();
        List<string> years = new List<string>();
        private string currentPcode = "";
        private void btnbutton2_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(
                () =>
                {
                    pcodes.Clear();
                    years.Clear();

                    years.Add("2007");
                    years.Add("2008");
                    years.Add("2009");
                    years.Add("2010");
                    years.Add("2011");
                    years.Add("2012");

                    //生成人员列表
                    //生成年份列表
                    for (int i = 0; i < 13000; i++)
                    {
                        pcodes.Add(string.Format("{0:00000}", i));
                    }

                    foreach (var pcode in pcodes)
                    {
                        currentPcode = pcode;
                        foreach (var y in years)
                        {
                            year = y;
                            pCode = pcode;
                            this.Invoke((Action)(() =>
                                             {
                                                 if (rtbrichTextBox1.Lines.Length > 500)
                                                 {
                                                     rtbrichTextBox1.Clear();
                                                 }
                                                 rtbrichTextBox1.Text += "当前:" + currentPcode + ":" + year + "\r\n";

                                             }));
                            GetData(null);

                            Thread.Sleep(r.Next(200, 1000));
                        }
                    }
                }
                );
        }
        Random r = new Random();
    }


}
